// ? 获取input框
let ipts = document.querySelectorAll(".userCard__log-box input");

let isUsername = false;
let isPassword = false;

// ? 用户名
console.log(ipts[0]);
ipts[0].onchange = function () {
  let username = ipts[0].value;
  let reg = /^\w{3,15}$/;
  if (reg.test(username)) {
    /* 
        检测用户名是否存在

        http://jx.xuzhixiang.top/ap/api/checkname.php?username=1
        */
    let username = ipts[0].value;
    let changeNameApi = "http://jx.xuzhixiang.top/ap/api/checkname.php";
    axios.get(changeNameApi, { params: { username } }).then((r) => {
      console.log(r.data);
      if (r.data.code === 1) {
        this.nextElementSibling.innerHTML = r.data.msg;
        this.nextElementSibling.style.color = "green";
        isUsername = true;
      } else {
        this.nextElementSibling.innerHTML = r.data.msg;
        this.nextElementSibling.style.color = "red";
        isUsername = false;
      }
    });
  } else {
    this.nextElementSibling.innerHTML = "格式不正确";
    this.nextElementSibling.style.color = "red";
    isUsername = false;
  }
};

// ? 密码
ipts[1].onchange = function () {
  let password = ipts[1].value;
  let reg = /^.{3,18}$/;
  if (reg.test(password)) {
    this.nextElementSibling.innerHTML = "格式正确";
    this.nextElementSibling.style.color = "green";
    isPassword = true;
  } else {
    this.nextElementSibling.innerHTML = "格式不正确";
    this.nextElementSibling.style.color = "red";
    isPassword = false;
  }
};

// ? 注册
ipts[2].onclick = function(){
    if(isUsername && isPassword){
        let username = ipts[0].value;
        let password = ipts[1].value;
        /* 
        注册接口

    接口地址：http://jx.xuzhixiang.top/ap/api/reg.php
    接口请求方式：get
    接口参数：
         username用户名
         password密码
    使用方式
         http://jx.xuzhixiang.top/ap/api/reg.php?username=11&password=1212

    服务器返回json数据
        */
        let regApi = "http://jx.xuzhixiang.top/ap/api/reg.php";
        axios.get(regApi,{params:{username,password}}).then(r=>{
            console.log(r.data);
            alert(r.data.msg);
            location.href = "login.html";
        })
    }
}
